﻿using System;
using System.Windows;
using System.Windows.Controls;

using advadev.DataStorage;


namespace GenericDataStorageDemo.WPF
{

    /// <summary>
    /// Represents the main window
    /// </summary>
    public partial class MainWindow : Window
    {

        /// <summary>
        /// Initializes a new instance of the MainWindow class
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            DataContext = MainViewModel.Current;
            DataStorage = new ServiceDataStorage("DEMO", "memoryDataStorageServiceEndpoint", "serviceUsername", "servicePassword");
            updateUsersList();
        }


        /// <summary>
        /// Gets the data storage
        /// </summary>
        private IDataStorage DataStorage
        {
            get;
            set;
        }


        /// <summary>
        /// Handles the "Click" event of the find button
        /// </summary>
        /// <param name="sender">
        /// Sender of the event
        /// </param>
        /// <param name="args">
        /// Event argument
        /// </param>
        private void FindButton_Click(object sender, RoutedEventArgs args)
        {
            if (!string.IsNullOrEmpty(idTextBox.Text))
            {
                User user = DataStorage.Find<User>(idTextBox.Text);
                if (user != null)
                {
                    idTextBox.Text = user.Id;
                    nameTextBox.Text = user.Name;
                }
                else
                {
                    nameTextBox.Text = "";
                }
            }
        }

        /// <summary>
        /// Handles the "Click" event of the save button
        /// </summary>
        /// <param name="sender">
        /// Sender of the event
        /// </param>
        /// <param name="args">
        /// Event argument
        /// </param>
        private void SaveButton_Click(object sender, RoutedEventArgs args)
        {
            if (!string.IsNullOrEmpty(idTextBox.Text) && !string.IsNullOrEmpty(nameTextBox.Text))
            {
                User user = new User(idTextBox.Text, nameTextBox.Text);
                DataStorage.Save<User>(user);
                idTextBox.Text = string.Empty;
                nameTextBox.Text = string.Empty;
                updateUsersList();
            }
        }

        /// <summary>
        /// Handles the "Click" event of the delete button
        /// </summary>
        /// <param name="sender">
        /// Sender of the event
        /// </param>
        /// <param name="args">
        /// Event argument
        /// </param>
        private void DeleteButton_Click(object sender, RoutedEventArgs args)
        {
            Button button = sender as Button;
            if (button != null)
            {
                User user = button.DataContext as User;
                if (user != null)
                {
                    DataStorage.Delete<User>(user);
                    updateUsersList();
                }
            }
        }

        /// <summary>
        /// Updates the users list
        /// </summary>
        private void updateUsersList()
        {
            MainViewModel.Current.Users.Clear();
            try
            {
                foreach (User user in DataStorage.Select<User>())
                {
                    MainViewModel.Current.Users.Add(user);
                }
            }
            catch { }
        }

    }

}
